package cgi.db;

import org.jdom.Element;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.util.List;
import java.util.Iterator;
import java.io.File;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: darrell
 * Date: Jun 14, 2004
 * Time: 6:35:26 PM
 * To change this template use File | Settings | File Templates.
 */
public class DBListParser {
	private class DatabaseListing {
		public static final String ELEMENT_NAME = "database";

		public static final String ATTRIBUTE_NAME = "name";
		private String name;
		public static final String ATTRIBUTE_ADDOPSPREFIX = "addopsprefix";
		private boolean addOpsPrefix;
		private String title;

		public DatabaseListing(Element e) {
			initialize(e);
		}

		private void initialize(Element e) {
			name = e.getAttributeValue(ATTRIBUTE_NAME);
			addOpsPrefix = Boolean.valueOf(e.getAttributeValue(ATTRIBUTE_ADDOPSPREFIX)).booleanValue();
			title = e.getTextTrim();
		}

		public String getTitle() {
			return title;
		}
		public String getName() {
			return name;
		}
		public boolean getAddOpsPrefix() {
			return addOpsPrefix;
		}
	}



	private DatabaseListing[] databases;

	public DBListParser() {
//		File f = new File(System.getProperty(ORACLE_PASSWORD_CHANGE_))
	}
	public DBListParser(Element root) {
		initialize(root);
	}

	public DBListParser(File f) throws JDOMException, IOException {
		Document d = new SAXBuilder().build(f);
		initialize(d.getRootElement());
	}

	private void initialize(Element root) {
		List databaseList = root.getChildren(DatabaseListing.ELEMENT_NAME);
		Iterator iterator = databaseList.iterator();
		databases = new DatabaseListing[databaseList.size()];
		for (int i=0; i<databases.length && iterator.hasNext(); i++)
			databases[i] = new DatabaseListing((Element)iterator.next());
	}
	public DatabaseListing[] getDatabaseListings() {
		return databases;
	}

}
